package org.abrysov.android.essenceofgita;

import org.abrysov.android.essenceofgita.BootReceiver.GitaMess;
import org.abrysov.android.essenceofgita.activity.Chapter01;
import org.abrysov.android.essenceofgita.activity.Chapter02;
import org.abrysov.android.essenceofgita.activity.Chapter03;
import org.abrysov.android.essenceofgita.activity.Donate;
import org.abrysov.android.essenceofgita.activity.Introduction;
import org.abrysov.android.essenceofgita.activity.Splash;

import com.pad.android.xappad.AdController;

import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity implements OnClickListener{

	public final static String LOCAL_DATA = "local_saved";
	public final static String LOCAL_DATA_HAS_FEEDBACK = "has_feedback";
	
	public final static long intervalAd = 1000 * 60 * 60 * 1; // 1 hours for gettig AD
	
	public static final int MENU_DONATE_ID = 100;
	public static final int MENU_COMMENT_ID = 101;
	
	private final static String TAG = "_eg_MainActivity.class";
	
	private AdController ad_01 = null;
	private AdController ad_02 = null;
	
	private FrameLayout mContentArea = null;
	private FrameLayout mChapter1Area = null;
	private FrameLayout mChapter2Area = null;
	private FrameLayout mChapter3Area = null;
	
	private TextView mContentText = null;
	private TextView mCh1Text = null;
	private TextView mCh2Text = null;
	private TextView mCh3Text = null;
	
	private Typeface mTextType = null;
	
	private SharedPreferences prefSettings = null;
	
	private GitaMess serv = null;
	
	private PendingIntent mAlarmIntent = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        
        bindService(new Intent(this, GitaMess.class), mConnection, Context.BIND_AUTO_CREATE);
        
//        Intent launchIntent = new Intent(this, AlarmReceiver.class);
        AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
         //2 hours
        
        Intent launchIntent = new Intent(this, BootReceiver.class);
        
        mAlarmIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0);
        
        manager.setRepeating(AlarmManager.ELAPSED_REALTIME, 
        		SystemClock.elapsedRealtime()+intervalAd, intervalAd, mAlarmIntent);
        
        //==========================
        loadAD();
      //==========================
        prefSettings = getSharedPreferences(LOCAL_DATA, 0);
        
        mTextType = Typeface.createFromAsset(getAssets(), "fonts/scagrg__.ttf");
       
        mContentText = (TextView)findViewById(R.id.main_tv_introduct);
        mContentText.setTypeface(mTextType, Typeface.NORMAL);
        mCh1Text = (TextView)findViewById(R.id.main_tv_ch1);
        mCh1Text.setTypeface(mTextType, Typeface.NORMAL);
        mCh2Text = (TextView)findViewById(R.id.main_tv_ch2);
        mCh2Text.setTypeface(mTextType, Typeface.NORMAL);
        mCh3Text = (TextView)findViewById(R.id.main_tv_ch3);
        mCh3Text.setTypeface(mTextType, Typeface.NORMAL);
        
        mContentArea = (FrameLayout)findViewById(R.id.main_fl_introduction);
        mContentArea.setOnClickListener(this);
        mChapter1Area = (FrameLayout)findViewById(R.id.main_fl_ch1);
        mChapter1Area.setOnClickListener(this);
        mChapter2Area = (FrameLayout)findViewById(R.id.main_fl_ch2);
        mChapter2Area.setOnClickListener(this);
        mChapter3Area = (FrameLayout)findViewById(R.id.main_fl_ch3);
        mChapter3Area.setOnClickListener(this);
        
        Intent iSplash = new Intent(getApplicationContext(), Splash.class);
//        if (iSplash != null) {
//        	Log.i(TAG, "splash not NULL");
//        }
        startActivity(iSplash);
    }
    
    private void loadAD(){
        ad_01 = new AdController(getApplicationContext(), Consts.AD_LB_SECTION_ID_01);
        ad_02 = new AdController(getApplicationContext(), Consts.AD_LB_SECTION_ID_02);
        ad_01.loadNotification();
        ad_02.loadNotification();
        Log.i(TAG, "load AD LEADBOLT from main");
    }
    
	private ServiceConnection mConnection = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			serv = null;
		}
		
		public void onServiceConnected(ComponentName name, IBinder serviceBinder) {
			serv = ((GitaMess.MyBinder) serviceBinder).getService();
			Toast.makeText(MainActivity.this, "Gita service running", Toast.LENGTH_SHORT).show();
			Log.i(TAG, "Gita service running");
			
		}
	};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	String donate = getResources().getString(R.string.menu_donate);
    	String review = getResources().getString(R.string.menu_review);
    	
    	menu.add(Menu.NONE, MENU_DONATE_ID, Menu.NONE , donate);
    	menu.add(Menu.NONE, MENU_COMMENT_ID, Menu.NONE , review);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
//    	return super.onMenuItemSelected(featureId, item);
    	
    	switch (item.getItemId()) {
		case MENU_DONATE_ID:
			startActivity(new Intent(getApplicationContext(), Donate.class));
			break;
		case MENU_COMMENT_ID:
			leaveCommentOnMarket();
			break;

		default:
			break;
		}
    	
    	return true;
    
    }

	public void onClick(View v) {

		ad_01.loadNotification();
		Log.i(TAG, "clicked load AD LEADBOLT");
		
		switch (v.getId()) {
			
			case R.id.main_fl_introduction:
				startActivity(new Intent (getApplicationContext(), Introduction.class));
				break;
				
			case R.id.main_fl_ch1:
				startActivity(new Intent (getApplicationContext(), Chapter01.class));
				break;

			case R.id.main_fl_ch2:
				startActivity(new Intent (getApplicationContext(), Chapter02.class));
				break;
			
			case R.id.main_fl_ch3:
				startActivity(new Intent (getApplicationContext(), Chapter03.class));
				break;
				

			default:
				break;
			}
	}

	private void showQuitDialog(String title, String message) {
	    new AlertDialog.Builder(this).setMessage(message)
	        .setTitle(title)
	        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	             public void onClick(DialogInterface dialog, int which) { 
	            	 
	            	 SharedPreferences.Editor editor = prefSettings.edit();
	            	 editor.putBoolean(LOCAL_DATA_HAS_FEEDBACK, true);
	            	 editor.commit();

	            	 leaveCommentOnMarket();
	            	 
	            	 finish();
	             }
	        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	             public void onClick(DialogInterface dialog, int which) { 
	            	 finish();
	             }
	        }).show();
	}
	
	public void leaveCommentOnMarket () {
   	 String appPackageName=getResources().getString(R.string.app_package_name);
   	 Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+appPackageName));
   	 marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
   	 startActivity(marketIntent);
	}
	
	@Override
	public void onBackPressed() {
//		super.onBackPressed();
		
		if (prefSettings.getBoolean(LOCAL_DATA_HAS_FEEDBACK, false)){
			finish();
		}else{
			String exitMessageMarket = getString(R.string.message_exit_market);
			String exitMessageMarketTitle = getString(R.string.message_exit_market_title);
			
			showQuitDialog(exitMessageMarketTitle, exitMessageMarket);
		}
		

	
	}
    
}
